<?php
class GroupsController extends AppController {
	var $name = 'Groups';
	function beforeFilter(){
		parent::beforeFilter();
		if (substr($this->params['action'], 0, 6) == 'admin_')
			$this->layout = "admin";
		else $this->layout = "lam";
	}	
	function admin_index() {
		$groups = $this->paginate();
		$this->set('groups', $groups);
	}
	
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid group', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->paginate = array(
			'User' => array(
				'conditions' => array('group_id' => $id),
				'limit' => 20,
				'order' => 'username'
			)
		);
		$this->set('users', $this->paginate('User'));
		$this->Group->recursiver = 0;
		$group = $this->Group->read(null,$id);
		$this->set('group',$group);
	}
	function admin_add() {
		if (!empty($this->data)) {
			$this->Group->create();
			if ($this->Group->save($this->data)) {
				$this->Session->setFlash(__('The group has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The group could not be saved. Please, try again.', true));
			}
		}
	}
	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid group', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Group->save($this->data)) {
				$this->Session->setFlash(__('The group has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The group could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Group->read(null, $id);
		}
	}
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for group', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Group->delete($id)) {
			$this->Session->setFlash(__('Group deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Group was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}	
}
?>